package com.tanxiuxin.YiWaiMai.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanxiuxin.YiWaiMai.common.Result;
import com.tanxiuxin.YiWaiMai.dto.DishDto;
import com.tanxiuxin.YiWaiMai.mapper.DishMapper;
import com.tanxiuxin.YiWaiMai.pojo.Dish;
import com.tanxiuxin.YiWaiMai.pojo.DishFlavor;
import com.tanxiuxin.YiWaiMai.pojo.SetmealDish;
import com.tanxiuxin.YiWaiMai.service.DishFlavorService;
import com.tanxiuxin.YiWaiMai.service.DishService;
import com.tanxiuxin.YiWaiMai.service.SetmealDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService{

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     *     新增菜品，同时保存对应的口味信息
     * @param dishDto
     */
    @Transactional
    public void saveAndFlavor(DishDto dishDto) {
        // 保存菜品的基本信息到到菜品表 Dish
        this.save(dishDto);

        Long dtoId = dishDto.getId(); // 菜品 id

        // 菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item) ->{
            item.setDishId(dtoId);
            return item;
        }).collect(Collectors.toList());

        // 保存菜品口味数据到菜品口味表 dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    /**
     *       根据 id 查询菜品信息以及对应的口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {

        // 1. 查询菜品基本信息，从 dish 表查询
        Dish dish = this.getById(id);

        // 拷贝信息
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish , dishDto);
        // 2. 查询当前菜品对应的口味信息，从 dish_flavor 表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());

        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        // 添加口味信息
        dishDto.setFlavors(flavors);
        return dishDto;
    }


    /**
     *      修改菜品信息
     * @param dishDto
     */
    @Transactional
    @Override
    public void updateWithFlavor(DishDto dishDto) {

        // 先更新 dish 表信息
        this.updateById(dishDto);

        // 清理当前菜品对应的口味信息    dish_flavor 表的 delete 操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId , dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        // 添加当前提交过来的口味数据     dish_flavor 表的 insert 操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item) ->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    /**
     *   删除菜品信息，同时删除对应的口味
     */
    @Transactional
    @Override
    public Result<String> removeAndFlavor(Long... ids) {
        // 单个删除，判断是否为停售
        // 批量删除
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getDishId,ids);
        List<SetmealDish> setmealDishes = setmealDishService.list(setmealDishLambdaQueryWrapper);
        List<Dish> list = new ArrayList<>();
        if (setmealDishes.size() == 0) { // 都不在套餐中
            LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(Dish::getId , ids);
            list = dishService.list(queryWrapper);
            // 判断这些不在套餐中是否为停售
            for (Dish dish : list) {
                if (dish.getStatus() == 0){ // 判断状态
                    // 说明都可以直接删除，同时删除对应的菜品数据
                    dishService.removeById(dish);
                    LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    dishFlavorLambdaQueryWrapper.in(DishFlavor::getDishId , ids);
                    dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
                    dishService.update();
                    dishFlavorService.update();
                    return Result.success("删除成功");
                }
                    return Result.error("有在售菜品，无法删除");
            }
        }
       return Result.error("有在售菜品，无法删除");
    }
}
